<?php
/**
 * $Id: backend_controller.php 206 2012-05-22 11:08:08Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 206 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2012-05-22 16:38:08 +0530 (Tue, 22 May 2012) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Backend controller for authorization of administrator
 *
 */
class BackendController extends BackendAppController
{
	var $name = 'Backend';
	var $helpers = array('Javascript','Form', 'Cms.Ajax');
	var $components = array('Cookie', 'RequestHandler');
	var $uses = array();

	function admin_login() 
	{
		// if already logged in, pass on to backend area
		if($this->Session->read('admin_user'))
			$this->redirect(Configure::read('Backend.adminLoginRedirect'));
	
		// login if cookie exists
		if($userInfo = $this->Cookie->read('super_admin_login_info'))
		{
			if(!empty($userInfo['name']) && !empty($userInfo['password']))
			{	
				$this->data['Admin'] = $userInfo;
			} 		
		}
			
		if($this->data)
		{
			$loggedIn = false;
			$superAdmin = false;
			$sysAdmin = false;
			
			// admins authorization
			if(Configure::read('Backend.adminDb') && $this->_checkDbAdmin())
			{
				$loggedIn = true;
			}
			if($this->_checkAdmin())
			{
				$loggedIn = true;
				$superAdmin = true; // hardcoded for now - change it for normal users
			}
			if($this->_checkDevAdmin())
			{
				$loggedIn = true;
				$superAdmin = true;
				$sysAdmin = true;
			}
			// code to create cookie
			if(isset($this->data['Admin']['remember_me']) && $this->data['Admin']['remember_me']==1 && $loggedIn  == true)
			{
				$cookie['name'] = $this->data['Admin']['name'];
				$cookie['password'] = $this->data['Admin']['password'];
				$this->Cookie->write('super_admin_login_info', $cookie,true, '+2 weeks');
			}
			
			if($loggedIn)
			{
                $this->Session->write('admin_user', $this->data['Admin']['name']);
				if($superAdmin)
				{
					$this->Session->write('super_admin', 'true');
				}
				if($sysAdmin)
				{
					$this->Session->write('sys_admin', 'true');
				}
				callHooks('AfterAdminLogin','.+',$this);
				$this->successMsg(__('Login successful.'));
				$this->redirect(Configure::read('Backend.adminLoginRedirect'));
			}
			else
			{
				$this->errorMsg(__('Please enter correct login information.'));		
				$this->redirect(Configure::read('Backend.adminLoginAction'));	
			}
		}
	}
	
	function _checkDbAdmin()
	{
		// Todo: make it md5 later
		$findConditions = array(
				array('or'=>array('Administrator.username'=> $this->data['Admin']['name'],'Administrator.email'=> $this->data['Admin']['name'])),
				array('or'=>array(
							'Administrator.password'=> $this->data['Admin']['password'],
							'Administrator.password'=> Security::hash($this->data['Admin']['password'],null,true)
						)));
		$admin = ClassRegistry::init('Backend.Administrator')->find('first', array('conditions'=>$findConditions));
		if($admin)
		{
			$this->Session->write('Auth.Administrator',$admin['Administrator']);
			return true;
		}
		return false;
	}
	
	function _checkAdmin()
	{
		//echo Security::hash($this->data['Admin']['password'], null, true); exit;
		if(($this->data['Admin']['name'] == Configure::read('cmsConfig.Backend.adminUser')) && ($this->data['Admin']['password'] == Configure::read('cmsConfig.Backend.adminPassword')))
		{
			return true;
		}
		elseif(($this->data['Admin']['name'] == Configure::read('cmsConfig.Backend.adminUser')) && (Security::hash($this->data['Admin']['password'], null, true) == Configure::read('cmsConfig.Backend.adminPassword')))
		{
			return true;
		}
		return false;
	}
	
	function _checkDevAdmin()
	{
		if(($this->data['Admin']['name'] == DEV_USER) && (md5($this->data['Admin']['password']) == DEV_PASS))
			return true;
		return false;
	}

	function admin_logout()
	{
		$this->Session->delete('admin_user');
		$this->Session->delete('super_admin');
		$this->Cookie->del('super_admin_login_info');
		callHooks('AfterAdminLogout','.+',$this);
		$this->redirect('/admin');
	}

	/**
	 * Enter description here...
	 *
	 */
	function admin_index() 
	{
		$this->pageTitle = __("Dashboard");

		/*
		$this->dashboard = array();
		callHooks('Dashboard', null, $this);
		//$this->set('dashboard_components', $this->dashboard);*/
   }
   
   function setAdminSession($testId)
   {
   		if($this->Session->read('tester_id') == $testId)
   		{
			$this->Session->write('admin_user', 'selenium');
			echo "logged in";
   		}
   		exit;
   }

}

?>